#!/bin/sh
#
# uwsgi - this script starts and stops the uwsgi emperor
#
# chkconfig:   - 85 15
# description: Fast, self-healing, application container server
# processname: uwsgi
# config:      /etc/uwsgi.ini
# config:      /etc/uwsgi.d

# Source function library.
. /etc/rc.d/init.d/functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
PROG=/usr/sbin/uwsgi
OWNER=uwsgi
NAME=uwsgi
DESC="Fast, self-healing, application container server"
DAEMON_OPTS="--ini /etc/uwsgi.ini --daemonize /var/log/uwsgi.log"

[ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi

lockfile=/var/lock/subsys/uwsgi

start () {
  echo -n "Starting $NAME $DESC: "
  daemon $PROG $DAEMON_OPTS
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $lockfile
  return $retval
}

stop () {
  echo -n "Stopping $NAME $DESC: "
  # uWSGI docs say INT is a gentler way to stop
  killproc $PROG -INT
  retval=$?
  echo
  [ $retval -eq 0 ] && rm -f $lockfile
  return $retval
}

reload () {
  echo "Reloading $NAME" 
  killproc $PROG -HUP
  RETVAL=$?
  echo
}

restart () {
    stop
    start
}

rh_status () {
  status $PROG
}

rh_status_q() {
  rh_status >/dev/null 2>&1
}

case "$1" in
  start)
    rh_status_q && exit 0
    $1
    ;;
  stop)
    rh_status_q || exit 0
    $1
    ;;
  restart)
    $1
    ;;
  reload)
    rh_status_q || exit 7
    $1
    ;;
  status)
    rh_status
    ;;
  condrestart|try-restart)
    rh_status_q || exit 0
    restart
    ;;
  *)  
    echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|status}" >&2
    exit 2
    ;;
esac
exit 0
